package com.whu.zqm.controller;

import com.whu.zqm.entity.Products;
import com.whu.zqm.entity.Role;
import com.whu.zqm.entity.User;
import com.whu.zqm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    public ResponseEntity<User> getUser(@PathVariable String name) {
        User u = userService.getUser(name);
        if (u == null) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.ok(u);
        }
    }

    @PostMapping("/add")
    public ResponseEntity<String> addUser(@RequestBody User user) {
        try {
            User u = userService.addUser(user);
            return ResponseEntity.ok("添加成功！" + u.getName());
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}
